﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using Mapster;
using Masuit.Tools.Files;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;
using XREditor.Dtos;
using XREditor.Models;

namespace XREditor.Controllers
{
    /// <summary>
    /// 最后一次操作的项目
    /// </summary>
    [DynamicApiController]
    public class ProjectHistoryController
    {
        private readonly ILogger<ProjectController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        private readonly ISevenZipCompressor _sevenZipCompressor;
        public ProjectHistoryController(ISqlSugarClient sqlSugarClient, ISevenZipCompressor sevenZipCompressor)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
            _sevenZipCompressor = sevenZipCompressor;
        }

        /// <summary>
        /// 修改最后操作的项目
        /// </summary>
        /// <param name="uifd">参照下方Schemas中：ProjectInfoFormDto</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] ProjectHistoryInfoFormDto phifd)
        {
            await db.Updateable<ProjectHistory>()
                .SetColumns(ph => ph.LastOpId == phifd.LastOpId)
                .Where(ph => ph.Id == 1)
                .ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = phifd,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看最后操作的项目
        /// </summary>
        /// <param name="pfd"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show()
        {
            ProjectHistory projectHistory = await db.Queryable<ProjectHistory>()
                .InSingleAsync(1);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = projectHistory.Adapt<ProjectHistoryInfoFormDto>(),
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
